dojo.provide("apt.Event");
dojo.require("apt.EventPopover");
dojo.require("dijit._WidgetBase");
dojo.require("dijit._TemplatedMixin");

dojo.declare("apt.Event", [dijit._WidgetBase, dijit._TemplatedMixin], {
    templateString: "<div class=\"event\" style=\"overflow: hidden\"><span class=\"info\">${info}</span></div>",
    widgetsInTemplate: false,
    info: '',
    type: '',
    parent: '',

    constructor: function (data) {
        this.inherited(arguments);
        dojo.safeMixin(this, data);
        if (this.type == 'reservation') {
            this.info = this.e.ReservationInfo.LicenseNumber;
            //if (this.e.ReservationInfo.hasOwnProperty('Vehicle') && this.e.ReservationInfo.Vehicle.hasOwnProperty('LicenseNumber') && this.e.ReservationInfo.Vehicle.LicenseNumber.length > 0) {
            //    this.info = this.e.ReservationInfo.Vehicle.LicenseNumber;
            //}
            // this.info = this.info + "Start: " + data.e.StartTimeJSDate.getHours() + ":" + data.e.StartTimeJSDate.getMinutes();
            // this.info = this.info + "  End: " + data.e.EndTimeJSDate.getHours() + ":" + data.e.EndTimeJSDate.getMinutes();
        } else {
            this.info = '';
        }
        // if (data.e.hasOwnProperty('PickupTime')){
        //   this.info = data.e.PickupTime + " " + data.e.EndHour;
        // }

        // this.info = this.info + "<br/> Duration: " + data.e.Duration;
    },

    postCreate: function () {
        if (this.type == "reservation") {
            //var hue = parseInt(this.e.ReservationNumber.toString().split("").reverse().join("").substr(0, 3));
            //debugger;
            //hue = hue % 360;
            var statusColor = {
            'Waiting' : {
                    color: 'black'
                    , background: '#C0C0C0'
                }, 'Late': {
                    color: 'white'
                    , background: 'red'
                }, 'IsWorkOrder': {
                color: 'white'
                    , background: '#FFA500'
                }, 'Arrived': {
                    color: 'white'
                    , background: '#800080'
                }, 'In progress': {
                    color: 'black'
                    , background: 'yellow'
                }, 'Interrupted': {
                    color: 'red'
                    , background: 'yellow'
                }, 'Done': {
                    color: 'black'
                    , background: '#00FF00'
                }, 'Ready': {
                    color: 'white'
                    , background: '#008000'
                }, 'Handovered': {
                    color: 'white'
                    , background: '#306EFF'
                }, 'Invoiced': {
                    color: 'white'
                    , background: '#808080'
                }, '': {
                    color: 'white'
                    , background: 'black'
                }
            };
            var status = (this.e.ReservationInfo && this.e.ReservationInfo.Status) || 'Waiting';
            var pickcolor = statusColor[status];
            dojo.style(this.domNode, 'background', pickcolor.background);
            dojo.style(this.domNode, 'color', pickcolor.color);
            dojo.style(this.domNode, 'border-color', 'black');
        }
    },

    showInfo: function (event) {
        if (this.type == 'reservation') {
            var info = this.e.ReservationInfo;
            //debugger;
            if (typeof document.popover == "object") {
                document.popover.destroyRecursive();
            }
            document.popover = new apt.EventPopover({
                title: "Reservation Info",
                contact: '',
                license: info.LicenseNumber,
                reservationNumber: info.WorkorderNo || this.e.ReservationNumber,
                start: this.e.StartTimeJSDate,
                end: this.e.EndTimeJSDate,
                duration: info.Duration,
                pickupDate: info.PickupDate,
                workOrder: info.WorkorderNo,
                transactionType: info.TransactionType,
                model: info.Model,
                custNoName: info.CustomerNo + ' / ' + info.ConFName + ' ' + info.ConLName,
                contactName: info.ConFName + ' ' + info.ConLName,
                contactTel: info.ContactTel,
                contactEmail: info.ContactEmail,
                salesman: info.SName,
                description: info.WorkDescription

            });

            document.popover.show();
        }
    }
});
